

/**
 * @param {string} num1
 * @param {string} num2
 * @return {string}
 */
var multiply = function (num1, num2) {

    if (num1[0] === '0' || num2[0] === '0') return '0'

    var addStrings = function (num1, num2) {
        // 从最后一位开始，相加，记得进位
        let i = num1.length - 1, j = num2.length - 1
        let rslt = ''
        let m = 0
        while (i >= 0 || j >= 0 || m > 0) {
            let temp = Number(num1[i] || 0) + Number(num2[j] || 0) + m
            m = Math.floor(temp / 10)
            temp = m > 0 ? temp - 10 : temp
            rslt = temp + rslt
            i--
            j--
        }
        return rslt
    };
    // 竖式相乘
    let len1 = num1.length
    let len2 = num2.length
    let rsltArr = []
    let zero = ''
    for (let i = len2 - 1; i >= 0; i--) {
        let m = 0, j = len1 - 1, temp, tempStr = ''
        while (j >= 0 || m > 0) {
            temp = (num1[j] || 0) * (num2[i] || 0) + m
            m = Math.floor(temp / 10)
            temp = temp % 10
            tempStr = temp + tempStr
            j--
        }
        rsltArr.push(tempStr + zero)
        zero += '0'
    }

    return rsltArr.reduce((pre, cur) => {
        return addStrings(pre, cur)
    }, '0')
};

console.log(multiply('123', '456'))